package reversi;


import java.util.Vector;

public class CheapBlue
    implements ReversiAlgorithm
{

    public CheapBlue()
    {
        selectedMove = null;
        version = " v3";
    }

    public void requestMove(GameController gamecontroller)
    {
        running = false;
        gamecontroller.doMove(selectedMove);
    }

    public void init(GameController gamecontroller, GameState gamestate, int i, int j)
    {
        initstate = gamestate;
        myIndex = i;
        controller = gamecontroller;
        initialized = true;
    }

    public String getName()
    {
        return "Cheap Blue";
    }

    public void cleanup()
    {
    }

    public void run()
    {
        while(!initialized) ;
        initialized = false;
        running = true;
        selectedMove = null;
        totalNodes = 0;
        int i = 1;
        int j = initstate.getMarkCount(-1) >= 15 ? 16 : initstate.getMarkCount(-1) + 1;
        if(initstate.getPossibleMoveCount(myIndex) == 0)
            selectedMove = null;
        else
            do
            {
                if(!running || i >= j)
                    break;
                Move move = searchToDepth(i++);
                if(running)
                    selectedMove = move;
            } while(true);
        if(running)
            controller.doMove(selectedMove);
    }

    Move searchToDepth(int i)
    {
        Move move = null;
        double d = 0.0D;
        double d1 = (-1.0D / 0.0D);
        double d2 = (1.0D / 0.0D);
        Vector vector = initstate.getPossibleMoves(myIndex);
        nodes = 0;
        for(int j = 0; j < vector.size(); j++)
        {
            Move move1 = (Move)vector.elementAt(j);
            if(running)
                if(i > 1)
                    d = alphaBetaMin(initstate.getNewInstance(move1), i - 1, d1, d2);
                else
                    d = calcScore(initstate.getNewInstance(move1), myIndex);
            if(d > d1)
            {
                move = move1;
                d1 = d;
            }
        }

        return move;
    }

    double alphaBetaMin(GameState gamestate, int i, double d, double d1)
    {
        double d2 = 0.0D;
        Vector vector = gamestate.getPossibleMoves(1 - myIndex);
        int j = 0;
        do
        {
            if(j >= vector.size())
                break;
            Move move = (Move)vector.elementAt(j);
            if(running)
                if(i > 1)
                    d2 = alphaBetaMax(gamestate.getNewInstance(move), i - 1, d, d1, false);
                else
                    return calcScore(gamestate.getNewInstance(move), myIndex);
            d1 = d2 >= d1 ? d1 : d2;
            if(d1 <= d)
                break;
            j++;
        } while(true);
        if(vector.size() == 0)
            d1 = alphaBetaMax(gamestate, i, d, d1, true);
        return d1;
    }

    double alphaBetaMax(GameState gamestate, int i, double d, double d1, boolean flag)
    {
        double d2 = 0.0D;
        Vector vector = gamestate.getPossibleMoves(myIndex);
        int j = 0;
        do
        {
            if(j >= vector.size())
                break;
            Move move = (Move)vector.elementAt(j);
            if(running)
                if(i > 1)
                    d2 = alphaBetaMin(gamestate.getNewInstance(move), i - 1, d, d1);
                else
                    return calcScore(gamestate.getNewInstance(move), myIndex);
            d = d2 <= d ? d : d2;
            if(d >= d1)
                break;
            j++;
        } while(true);
        if(vector.size() == 0)
            if(flag)
                d = gamestate.getMarkCount(myIndex) <= gamestate.getMarkCount((myIndex + 1) % 2) ? -99999D : 99999D;
            else
                d = alphaBetaMin(gamestate, i, d, d1);
        return d;
    }

    private double calcScore(GameState gamestate, int i)
    {
        int j = 0;
        int k = 0;
        int l = 0;
        int i1 = 0;
        int j1 = 0;
        int k1 = 0;
        if(i == 0)
            k1 = 1;
        int l1 = gamestate.getPossibleMoveCount(i);
        int i2 = gamestate.getPossibleMoveCount(k1);
        int j2 = (l1 - i2) * 4;
        int k2 = gamestate.getMarkCount(k1);
        int l2 = gamestate.getMarkCount(i);
        boolean flag = false;
        if(k2 + l2 == 64)
        {
            if(k2 < l2)
                return 10000D;
            if(k2 > l2)
                return -10000D;
        } else
        {
            if(k2 == 0)
                return 10000D;
            if(l2 == 0)
                return -10000D;
        }
        if(gamestate.getMarkAt(0, 0) == i)
        {
            j += 20;
            if(gamestate.getMarkAt(1, 0) == i)
            {
                k += 10;
                if(gamestate.getMarkAt(2, 0) == i)
                {
                    k += 10;
                    if(gamestate.getMarkAt(3, 0) == i)
                    {
                        k += 10;
                        if(gamestate.getMarkAt(4, 0) == i)
                        {
                            k += 10;
                            if(gamestate.getMarkAt(5, 0) == i)
                            {
                                k += 10;
                                if(gamestate.getMarkAt(6, 0) == i)
                                    k += 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(0, 1) == i)
            {
                k += 10;
                if(gamestate.getMarkAt(0, 2) == i)
                {
                    k += 10;
                    if(gamestate.getMarkAt(0, 3) == i)
                    {
                        k += 10;
                        if(gamestate.getMarkAt(0, 4) == i)
                        {
                            k += 10;
                            if(gamestate.getMarkAt(0, 5) == i)
                            {
                                k += 10;
                                if(gamestate.getMarkAt(0, 6) == i)
                                    k += 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(1, 0) == i && gamestate.getMarkAt(0, 1) == i)
            {
                i1 += 5;
                if(gamestate.getMarkAt(2, 0) == i && gamestate.getMarkAt(1, 1) == i && gamestate.getMarkAt(0, 2) == i)
                {
                    i1 += 10;
                    if(gamestate.getMarkAt(3, 0) == i && gamestate.getMarkAt(2, 1) == i && gamestate.getMarkAt(1, 2) == i && gamestate.getMarkAt(0, 3) == i)
                        i1 += 50;
                }
            }
        } else
        if(gamestate.getMarkAt(0, 0) == k1)
        {
            j -= 20;
            if(gamestate.getMarkAt(1, 0) == k1)
            {
                k -= 10;
                if(gamestate.getMarkAt(2, 0) == k1)
                {
                    k -= 10;
                    if(gamestate.getMarkAt(3, 0) == k1)
                    {
                        k -= 10;
                        if(gamestate.getMarkAt(4, 0) == k1)
                        {
                            k -= 10;
                            if(gamestate.getMarkAt(5, 0) == k1)
                            {
                                k -= 10;
                                if(gamestate.getMarkAt(6, 0) == k1)
                                    k -= 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(1, 0) == k1)
            {
                k -= 10;
                if(gamestate.getMarkAt(2, 0) == k1)
                {
                    k -= 10;
                    if(gamestate.getMarkAt(3, 0) == k1)
                    {
                        k -= 10;
                        if(gamestate.getMarkAt(4, 0) == k1)
                        {
                            k -= 10;
                            if(gamestate.getMarkAt(5, 0) == k1)
                            {
                                k -= 10;
                                if(gamestate.getMarkAt(6, 0) == k1)
                                    k -= 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(1, 0) == k1 && gamestate.getMarkAt(0, 1) == k1)
            {
                i1 -= 5;
                if(gamestate.getMarkAt(2, 0) == k1 && gamestate.getMarkAt(1, 1) == k1 && gamestate.getMarkAt(0, 2) == k1)
                {
                    i1 -= 10;
                    if(gamestate.getMarkAt(3, 0) == k1 && gamestate.getMarkAt(2, 1) == k1 && gamestate.getMarkAt(1, 2) == k1 && gamestate.getMarkAt(0, 3) == k1)
                        i1 -= 50;
                }
            }
        } else
        {
            if(gamestate.getMarkAt(1, 1) == i)
                j1 -= 5;
            else
            if(gamestate.getMarkAt(1, 1) == k1)
                j1 += 5;
            if(gamestate.getMarkAt(2, 0) == i && gamestate.getMarkAt(2, 1) == i && gamestate.getMarkAt(2, 2) == i && gamestate.getMarkAt(1, 2) == i && gamestate.getMarkAt(0, 2) == i)
                l += 5;
            else
            if(gamestate.getMarkAt(2, 0) == k1 && gamestate.getMarkAt(2, 1) == k1 && gamestate.getMarkAt(2, 2) == k1 && gamestate.getMarkAt(1, 2) == k1 && gamestate.getMarkAt(0, 2) == k1)
                l -= 5;
        }
        if(gamestate.getMarkAt(7, 0) == i)
        {
            j += 20;
            if(gamestate.getMarkAt(6, 0) == i)
            {
                k += 10;
                if(gamestate.getMarkAt(5, 0) == i)
                {
                    k += 10;
                    if(gamestate.getMarkAt(4, 0) == i)
                    {
                        k += 10;
                        if(gamestate.getMarkAt(3, 0) == i)
                        {
                            k += 10;
                            if(gamestate.getMarkAt(2, 0) == i)
                            {
                                k += 10;
                                if(gamestate.getMarkAt(1, 0) == i)
                                    k += 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(7, 1) == i)
            {
                k += 10;
                if(gamestate.getMarkAt(7, 2) == i)
                {
                    k += 10;
                    if(gamestate.getMarkAt(7, 3) == i)
                    {
                        k += 10;
                        if(gamestate.getMarkAt(7, 4) == i)
                        {
                            k += 10;
                            if(gamestate.getMarkAt(7, 5) == i)
                            {
                                k += 10;
                                if(gamestate.getMarkAt(7, 6) == i)
                                    k += 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(6, 0) == i && gamestate.getMarkAt(7, 1) == i)
            {
                i1 += 5;
                if(gamestate.getMarkAt(5, 0) == i && gamestate.getMarkAt(6, 1) == i && gamestate.getMarkAt(7, 2) == i)
                {
                    i1 += 10;
                    if(gamestate.getMarkAt(4, 0) == i && gamestate.getMarkAt(5, 1) == i && gamestate.getMarkAt(6, 2) == i && gamestate.getMarkAt(7, 3) == i)
                        i1 += 50;
                }
            }
        } else
        if(gamestate.getMarkAt(7, 0) == k1)
        {
            j -= 20;
            if(gamestate.getMarkAt(6, 0) == k1)
            {
                k -= 10;
                if(gamestate.getMarkAt(5, 0) == k1)
                {
                    k -= 10;
                    if(gamestate.getMarkAt(4, 0) == k1)
                    {
                        k -= 10;
                        if(gamestate.getMarkAt(3, 0) == k1)
                        {
                            k -= 10;
                            if(gamestate.getMarkAt(2, 0) == k1)
                            {
                                k -= 10;
                                if(gamestate.getMarkAt(1, 0) == k1)
                                    k -= 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(7, 1) == k1)
            {
                k -= 10;
                if(gamestate.getMarkAt(7, 2) == k1)
                {
                    k -= 10;
                    if(gamestate.getMarkAt(7, 3) == k1)
                    {
                        k -= 10;
                        if(gamestate.getMarkAt(7, 4) == k1)
                        {
                            k -= 10;
                            if(gamestate.getMarkAt(7, 5) == k1)
                            {
                                k -= 10;
                                if(gamestate.getMarkAt(7, 6) == k1)
                                    k -= 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(6, 0) == k1 && gamestate.getMarkAt(7, 1) == k1)
            {
                i1 -= 5;
                if(gamestate.getMarkAt(5, 0) == k1 && gamestate.getMarkAt(6, 1) == k1 && gamestate.getMarkAt(7, 2) == k1)
                {
                    i1 -= 10;
                    if(gamestate.getMarkAt(4, 0) == k1 && gamestate.getMarkAt(5, 1) == k1 && gamestate.getMarkAt(6, 2) == k1 && gamestate.getMarkAt(7, 3) == k1)
                        i1 -= 50;
                }
            }
        } else
        {
            if(gamestate.getMarkAt(6, 1) == i)
                j1 -= 5;
            else
            if(gamestate.getMarkAt(6, 1) == k1)
                j1 += 5;
            if(gamestate.getMarkAt(5, 0) == i && gamestate.getMarkAt(5, 1) == i && gamestate.getMarkAt(5, 2) == i && gamestate.getMarkAt(6, 2) == i && gamestate.getMarkAt(7, 2) == i)
                l += 5;
            else
            if(gamestate.getMarkAt(5, 0) == k1 && gamestate.getMarkAt(5, 1) == k1 && gamestate.getMarkAt(5, 2) == k1 && gamestate.getMarkAt(6, 2) == k1 && gamestate.getMarkAt(7, 2) == k1)
                l += 5;
        }
        if(gamestate.getMarkAt(0, 7) == i)
        {
            j += 20;
            if(gamestate.getMarkAt(1, 7) == i)
            {
                k += 10;
                if(gamestate.getMarkAt(2, 7) == i)
                {
                    k += 10;
                    if(gamestate.getMarkAt(3, 7) == i)
                    {
                        k += 10;
                        if(gamestate.getMarkAt(4, 7) == i)
                        {
                            k += 10;
                            if(gamestate.getMarkAt(5, 7) == i)
                            {
                                k += 10;
                                if(gamestate.getMarkAt(6, 7) == i)
                                    k += 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(0, 6) == i)
            {
                k += 10;
                if(gamestate.getMarkAt(0, 5) == i)
                {
                    k += 10;
                    if(gamestate.getMarkAt(0, 4) == i)
                    {
                        k += 10;
                        if(gamestate.getMarkAt(0, 3) == i)
                        {
                            k += 10;
                            if(gamestate.getMarkAt(0, 2) == i)
                            {
                                k += 10;
                                if(gamestate.getMarkAt(0, 1) == i)
                                    k += 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(0, 6) == i && gamestate.getMarkAt(1, 7) == i)
            {
                i1 += 5;
                if(gamestate.getMarkAt(0, 5) == i && gamestate.getMarkAt(1, 6) == i && gamestate.getMarkAt(2, 7) == i)
                {
                    i1 += 10;
                    if(gamestate.getMarkAt(0, 4) == i && gamestate.getMarkAt(1, 5) == i && gamestate.getMarkAt(2, 6) == i && gamestate.getMarkAt(3, 7) == i)
                        i1 += 50;
                }
            }
        } else
        if(gamestate.getMarkAt(0, 7) == k1)
        {
            j -= 20;
            if(gamestate.getMarkAt(1, 7) == k1)
            {
                k -= 10;
                if(gamestate.getMarkAt(2, 7) == k1)
                {
                    k -= 10;
                    if(gamestate.getMarkAt(3, 7) == k1)
                    {
                        k -= 10;
                        if(gamestate.getMarkAt(4, 7) == k1)
                        {
                            k -= 10;
                            if(gamestate.getMarkAt(5, 7) == k1)
                            {
                                k -= 10;
                                if(gamestate.getMarkAt(6, 7) == k1)
                                    k -= 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(0, 6) == k1)
            {
                k -= 10;
                if(gamestate.getMarkAt(0, 5) == k1)
                {
                    k -= 10;
                    if(gamestate.getMarkAt(0, 4) == k1)
                    {
                        k -= 10;
                        if(gamestate.getMarkAt(0, 3) == k1)
                        {
                            k -= 10;
                            if(gamestate.getMarkAt(0, 2) == k1)
                            {
                                k -= 10;
                                if(gamestate.getMarkAt(0, 1) == k1)
                                    k -= 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(0, 6) == k1 && gamestate.getMarkAt(1, 7) == k1)
            {
                i1 -= 5;
                if(gamestate.getMarkAt(0, 5) == k1 && gamestate.getMarkAt(1, 6) == k1 && gamestate.getMarkAt(2, 7) == k1)
                {
                    i1 -= 10;
                    if(gamestate.getMarkAt(0, 4) == k1 && gamestate.getMarkAt(1, 5) == k1 && gamestate.getMarkAt(2, 6) == k1 && gamestate.getMarkAt(3, 7) == k1)
                        i1 -= 50;
                }
            }
        } else
        {
            if(gamestate.getMarkAt(1, 6) == i)
                j1 -= 5;
            else
            if(gamestate.getMarkAt(1, 6) == k1)
                j1 += 5;
            if(gamestate.getMarkAt(0, 5) == i && gamestate.getMarkAt(1, 5) == i && gamestate.getMarkAt(2, 5) == i && gamestate.getMarkAt(2, 6) == i && gamestate.getMarkAt(2, 7) == i)
                l += 5;
            else
            if(gamestate.getMarkAt(0, 5) == k1 && gamestate.getMarkAt(1, 5) == k1 && gamestate.getMarkAt(2, 5) == k1 && gamestate.getMarkAt(2, 6) == k1 && gamestate.getMarkAt(2, 7) == k1)
                l -= 5;
        }
        if(gamestate.getMarkAt(7, 7) == i)
        {
            j += 20;
            if(gamestate.getMarkAt(6, 7) == i)
            {
                k += 10;
                if(gamestate.getMarkAt(5, 7) == i)
                {
                    k += 10;
                    if(gamestate.getMarkAt(4, 7) == i)
                    {
                        k += 10;
                        if(gamestate.getMarkAt(3, 7) == i)
                        {
                            k += 10;
                            if(gamestate.getMarkAt(2, 7) == i)
                            {
                                k += 10;
                                if(gamestate.getMarkAt(1, 7) == i)
                                    k += 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(7, 6) == i)
            {
                k += 10;
                if(gamestate.getMarkAt(7, 5) == i)
                {
                    k += 10;
                    if(gamestate.getMarkAt(7, 4) == i)
                    {
                        k += 10;
                        if(gamestate.getMarkAt(7, 3) == i)
                        {
                            k += 10;
                            if(gamestate.getMarkAt(7, 2) == i)
                            {
                                k += 10;
                                if(gamestate.getMarkAt(7, 1) == i)
                                    k += 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(7, 6) == i && gamestate.getMarkAt(6, 7) == i)
            {
                i1 += 5;
                if(gamestate.getMarkAt(7, 5) == i && gamestate.getMarkAt(6, 6) == i && gamestate.getMarkAt(5, 7) == i)
                {
                    i1 += 10;
                    if(gamestate.getMarkAt(7, 4) == i && gamestate.getMarkAt(6, 5) == i && gamestate.getMarkAt(5, 6) == i && gamestate.getMarkAt(4, 7) == i)
                        i1 += 50;
                }
            }
        } else
        if(gamestate.getMarkAt(7, 7) == k1)
        {
            j -= 20;
            if(gamestate.getMarkAt(6, 7) == k1)
            {
                k -= 10;
                if(gamestate.getMarkAt(5, 7) == k1)
                {
                    k -= 10;
                    if(gamestate.getMarkAt(4, 7) == k1)
                    {
                        k -= 10;
                        if(gamestate.getMarkAt(3, 7) == k1)
                        {
                            k -= 10;
                            if(gamestate.getMarkAt(2, 7) == k1)
                            {
                                k -= 10;
                                if(gamestate.getMarkAt(1, 7) == k1)
                                    k -= 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(7, 6) == k1)
            {
                k -= 10;
                if(gamestate.getMarkAt(7, 5) == k1)
                {
                    k -= 10;
                    if(gamestate.getMarkAt(7, 4) == k1)
                    {
                        k -= 10;
                        if(gamestate.getMarkAt(7, 3) == k1)
                        {
                            k -= 10;
                            if(gamestate.getMarkAt(7, 2) == k1)
                            {
                                k -= 10;
                                if(gamestate.getMarkAt(7, 1) == k1)
                                    k -= 10;
                            }
                        }
                    }
                }
            }
            if(gamestate.getMarkAt(7, 6) == k1 && gamestate.getMarkAt(6, 7) == k1)
            {
                i1 -= 5;
                if(gamestate.getMarkAt(7, 5) == k1 && gamestate.getMarkAt(6, 6) == k1 && gamestate.getMarkAt(5, 7) == k1)
                {
                    i1 -= 10;
                    if(gamestate.getMarkAt(7, 4) == k1 && gamestate.getMarkAt(6, 5) == k1 && gamestate.getMarkAt(5, 6) == k1 && gamestate.getMarkAt(4, 7) == k1)
                        i1 -= 50;
                }
            }
        } else
        {
            if(gamestate.getMarkAt(6, 6) == i)
                j1 -= 5;
            else
            if(gamestate.getMarkAt(6, 6) == k1)
                j1 += 5;
            if(gamestate.getMarkAt(7, 5) == i && gamestate.getMarkAt(6, 5) == i && gamestate.getMarkAt(5, 5) == i && gamestate.getMarkAt(5, 6) == i && gamestate.getMarkAt(5, 7) == i)
                l += 5;
            else
            if(gamestate.getMarkAt(7, 5) == k1 && gamestate.getMarkAt(6, 5) == k1 && gamestate.getMarkAt(5, 5) == k1 && gamestate.getMarkAt(5, 6) == k1 && gamestate.getMarkAt(5, 7) == k1)
                l -= 5;
        }
        return (double)(j2 + j + k + l + j1 + i1);
    }

    private static final int WEIGHT_POSSIBLE_MOVES = 4;
    private static final int WIN_SCORE = 10000;
    private static final int CORNER_BONUS = 20;
    private static final int BORDER_BONUS = 10;
    private static final int CORNER_AREA_BONUS = 5;
    private static final int CORNER_TO_CENTRE_BONUS = 5;
    private static final int SAFE_AREA_BONUS_BASIC = 5;
    private static final int SAFE_AREA_BONUS_GOOD = 10;
    private static final int SAFE_AREA_BONUS_EXCELLENT = 50;
    private static final String NAME = "Cheap Blue";
    boolean initialized;
    boolean running;
    private GameController controller;
    private GameState initstate;
    private int myIndex;
    private int nodes;
    private int totalNodes;
    Move selectedMove;
    private String version;
}
